Skip to content

Populate dag and note before firing dag run state-change listeners#69873

Open
kacpermuda wants to merge 2 commits into
apache:mainfrom
kacpermuda:dag-listeners-extend
Open

Populate dag and note before firing dag run state-change listeners#69873
kacpermuda wants to merge 2 commits into
apache:mainfrom
kacpermuda:dag-listeners-extend

Conversation

@kacpermuda

@kacpermuda kacpermuda commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

When patching a Dag Run's state to success or failed via the API, two issues
existed with the listener call:

  1. dag_run.dag was always None when the listener received the object, because the route loads the dag separately but never attached it to the run. After the change, the listener will have access to SerializedDag that is used for example by OpenLineage listener to create an AirflowDagRunFacet.
  2. The listener message was an empty string. After the change, we will match the messages passed to task listener methods when manually changing task's state. It will make it easier for listener (and end users) to figure out what caused the early success / failure of dagrun.
  3. When both note and state were patched in the same request (dagrun state was set and note was provided), the note could be applied after the listener fired, so the listener saw the old / no note. Fixed by sorting the patch fields so note is always processed before state.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Sonnet 4.6
    Generated-by: Claude Sonnet 4.6 following the guidelines

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

Comment on lines +225 to +227
# Sort keys so that the "note" update happens before "state" update, so that the listeners called inside
# `patch_dag_run_state()` already receive updated DagRun object with note.
for attr_name, attr_value_raw in sorted(data.items()):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sorted(data.items()) to force "note" before "state" couples correctness to the field names' alphabetical order rather than expressing the ordering intent explicitly. This works today because "note" < "state", but is a trap for the next field added to DAGRunPatchBody & nothing would signal that ordering matters.

I suggest using an explicit ordering or just processing "note" before the loop which would be clearer and safer.

@pierrejeambrun pierrejeambrun Jul 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, we might have a similar problem on the Task Instance endpoint as well. (Just realized there is another PR opened for this)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense, improved this piece of code by removing the loop entirely and just ordering calls explicitly. @pierrejeambrun do you mind linking the taskinstance PR? Would love to track it and make sure these fixes are all merged, included in next release and backported.

@pierrejeambrun pierrejeambrun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks overall looking good beside Amogh's comment.

Comment on lines +225 to +227
# Sort keys so that the "note" update happens before "state" update, so that the listeners called inside
# `patch_dag_run_state()` already receive updated DagRun object with note.
for attr_name, attr_value_raw in sorted(data.items()):

@pierrejeambrun pierrejeambrun Jul 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, we might have a similar problem on the Task Instance endpoint as well. (Just realized there is another PR opened for this)

@kacpermuda kacpermuda force-pushed the dag-listeners-extend branch from 5704cd3 to 1cdf0dc Compare July 15, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants